package com.xjwn.datasync;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.xjwn.datasync.pojo.ScheduleSetting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.List;

//定时任务预热
//spring boot项目启动完成后，加载数据库里状态为正常的定时任务
//CommandLineRunner用于在应用程序启动后执行特定的代码逻辑,实现该接口并重写run方法就会执行run方法里的逻辑
@Service
@Slf4j
public class SysJobRunner implements CommandLineRunner {
  
    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;
  
    @Override  
    public void run(String... args) {  
        // 初始加载数据库里状态为正常的定时任务
        List<ScheduleSetting> jobList = Db.lambdaQuery(ScheduleSetting.class)
                .eq(ScheduleSetting::getJobStatus,1)
                .list();

        if (CollectionUtils.isNotEmpty(jobList)) {
            for (ScheduleSetting job : jobList) {
//                SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams(),job.getSdbUrl(),job.getTdbUrl(), job.getRemark(), job.getBeginTime(), job.getEndTime(), job.getProcName(), job.getPkName());
                cronTaskRegistrar.addCronTask(job);
            }  
            log.info("定时任务已加载完毕...");
        }
    }



} 